package xyz.ranyh.jobclient.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

/**
 * Created by rany on 12/23/16.
 */

public class UserParser {
    Editor editor = null;
    SharedPreferences userDetail = null;
    Context context = null;

    public UserParser() {
        this.context = Application.getContext();
        ;
        userDetail = this.context.getSharedPreferences("user", Context.MODE_PRIVATE);
        editor = this.userDetail.edit();

    }

    public User parse() {
        User user = new User();

        if (!userDetail.getString("email", "").isEmpty()) {
            user.setEmail(userDetail.getString("email", ""));
            user.setPassword(userDetail.getString("password", ""));
            user.setNickname(userDetail.getString("nickname", ""));
            user.setRole(userDetail.getString("role", ""));
            user.setCreateDate(userDetail.getString("createDate", ""));
        }

        return user;
    }

    public boolean save(User user) {
        editor.clear();
        editor.putString("email", user.getEmail());
        editor.putString("password", user.getPassword());
        editor.putString("nickname", user.getNickname());
        editor.putString("role", user.getRole());
        editor.putString("createDate", user.getCreateDate());

        return editor.commit();
    }

    public boolean clear() {
        editor.clear();
        editor.remove("email");
        editor.remove("password");
        editor.remove("nickname");
        editor.remove("role");
        editor.remove("createDate");
        return editor.commit();
    }
}
